<?php


namespace app\app\controller;


class Tiantang extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    /**qqq
     * @param $dateStart string 年月日
     */
    public function createDateArray($dateStart)
    {
        $dateStart = strtotime($dateStart);
        $dateArray = [];
        $time = time();
        $dayNum = ceil(($time - $dateStart) / (24 * 60 * 60));
        if ($dayNum > 40) {
            $dayNum = 40;
        }
//        selfDump($dayNum);
        for ($i = -1; $i < $dayNum; $i++) {

            $dateArray[] = date('Ymd', strtotime(($i < 0 ? "" : ' + ') . $i . ' days', $dateStart));
        }

        selfDump($dateArray);
        return $dateArray;

    }

    public function createKuku2Path($matchNam, $vodNames, $currentNum, $nextNum, $m3u8, $needDealArray, $isZongyi = false)
    {
        $rulesArray = [];
        if (!$isZongyi) {
            $rulesArray[] = '/' . $needDealArray[1] . '/{vodName}{nextNum}/{hls}index.m3u8';
            $rulesArray[] = '/{nextNum}/{vodName}{nextNum}/{hls}index.m3u8';
            $rulesArray[] = '/{vodName}{nextNum}/{hls}index.m3u8';

            $rulesArray[] = '/1000/{vodName}/{vodName}cc{nextNum}集/{hls}index.m3u8';
            $rulesArray[] = '/2000/{vodName}/{vodName}cc{nextNum}集/{hls}index.m3u8';

            $rulesArray[] = '/1000/{vodName}/{vodName}{nextNum}集/{hls}index.m3u8';
            $rulesArray[] = '/2000/{vodName}/{vodName}{nextNum}集/{hls}index.m3u8';

            $rulesArray[] = '/1000/{vodName}/{vodName}{nextNum}/{hls}index.m3u8';
            $rulesArray[] = '/2000/{vodName}/{vodName}{nextNum}/{hls}index.m3u8';

            $rulesArray[] = '/{vodName}/{vodName}{nextNum}/{hls}index.m3u8';
            $rulesArray[] = '/{vodName}/{vodName}{nextNum}/{hls}index.m3u8';

            $rulesArray[] = '/1000/{vodName}/{nextNum}/{hls}index.m3u8';
            $rulesArray[] = '/2000/{vodName}/{nextNum}/{hls}index.m3u8';

            $rulesArray[] = '/1200/{vodName}/{nextNum}/2000kb/{hls}index.m3u8';
            $rulesArray[] = '/{vodName}/{nextNum}/2000kb/{hls}index.m3u8';
            $rulesArray[] = '/{vodName}/{nextNum}/1000kb/{hls}index.m3u8';

            $returnArray[] = '/{vodName}/{nextNum}/{hls}index.m3u8';
            $rulesArray[] = '/{vodName}cc{nextNum}/{hls}index.m3u8';
            $rulesArray[] = '/{vodName}cc{nextNum}集/{hls}index.m3u8';
            $rulesArray[] = '/2017/{vodName}/{vodName} {nextNum}/{hls}index.m3u8';
        } else {

        }
        if ($needDealArray[2] ==
            'index.m3u8' || $needDealArray[2] == 'hls') {
            $urlVodName = $needDealArray[1];
        } else if ($needDealArray[3] == '1000kb' || $needDealArray[3] == '2000kb') {
            $urlVodName = $needDealArray[1];
        } else if ($needDealArray[1] == '1000' || $needDealArray[1] == '2000') {
            $urlVodName = $needDealArray[2];
        } else if ($needDealArray[3] == 'index.m3u8' || $needDealArray[3] == 'hls') {
            if (in_array($needDealArray[1], ['0718'])) {
                $urlVodName = $needDealArray[2];
            } else
                $urlVodName = $needDealArray[1];
        } else if ($needDealArray[4] == 'index.m3u8' || $needDealArray[4] == 'hls') {
            $urlVodName = $needDealArray[2];
            $urlVodName2 = $needDealArray[3];
            $urlVodName2 = str_replace($currentNum, '', $urlVodName2);
//            selfDump($needDealArray);
//            selfDump($needDealArray[3]);
//            selfDump($urlVodName2);
            array_unshift($rulesArray, '/' . $needDealArray[1] . '/{vodName}/{vodName}{nextNum}/{hls}index.m3u8');
            array_unshift($rulesArray, '/' . $needDealArray[1] . '/{vodName}/{vodName}{nextNum}/{hls}index.m3u8');
            array_unshift($rulesArray, '/' . $needDealArray[1] . '/{vodName}/{urlVodName2}{nextNum}/{hls}index.m3u8');
            array_unshift($rulesArray, '/' . $needDealArray[1] . '/{vodName}/{urlVodName2}{nextNum}/{hls}index.m3u8');

        }

        if (strpos($urlVodName, '第') > 0 && strpos($urlVodName, '期') > 0) {
            $nextNumArray = explode('第', $urlVodName);
            selfDump($nextNumArray);
            $nextNum = intval($nextNumArray[1]);
            selfDump("0000000999999", $nextNum);

            if (strpos($urlVodName, '期上') > 0) {
                selfDump("0000000");
                array_unshift($rulesArray, '/{vodName}第' . intval($currentNum) . '期/{hls}index.m3u8');
                array_unshift($rulesArray, '/{vodName}第' . intval($nextNum) . '期下/{hls}index.m3u8');
            } else if (strpos($urlVodName, '期下') > 0) {
                selfDump("3434343");
                array_unshift($rulesArray, '/{vodName}第' . intval($currentNum) . '期/{hls}index.m3u8');
                array_unshift($rulesArray, '/{vodName}第' . (intval($nextNum) + 1) . '期上/{hls}index.m3u8');
            } else {
                selfDump("444434343");
                array_unshift($rulesArray, '/{vodName}第' . $nextNum . '期/{hls}index.m3u8');
            }
        }
        selfDump("0-0-0-0-0-0-0-", $rulesArray);

        $urlVodName = str_replace(['[1000kb]', '[2000kb]'], ['', ''], $urlVodName);
        $urlVodNameArray = explode('cc', $urlVodName);
        $urlVodName = $urlVodNameArray[0];
        $urlVodName = str_replace($currentNum, '', $urlVodName);
        array_unshift($vodNames, $urlVodName);

        $vodNames = $this->dealVOdNameQuarter($vodNames);

        $hlsReplace = '';
        if (strpos($m3u8, '/hls/') > 0) {
            $hlsReplace = 'hls/';
        }


//        selfDump($hlsReplace);
        $rulesArray = str_replace(['{hls}', '{nextNum}', ' '], [$hlsReplace, $nextNum, '%20'], $rulesArray);
        if (isset($urlVodName2)) {
            $rulesArray = str_replace('{urlVodName2}', $urlVodName2, $rulesArray);
        }

        $newVodNames = [];
        foreach ($vodNames as $vodName) {
            $newVodName = preg_replace('/\d+/', '', $vodName);
            $newVodNames[] = trim($vodName);
            $newVodNames[] = trim($newVodName);
        }
        $newVodNames = array_filter($newVodNames);
        $newVodNames = array_unique($newVodNames);
        $returnArray = [];
        foreach ($newVodNames as $vodName) {
            $returnArray = array_merge($returnArray, str_replace('{vodName}', $vodName, $rulesArray));
        }
        return $returnArray;
    }


    public function create147PcPath($vodName, $vodName2, $nextNum, $m3u8)
    {
        //http://v1.zxjftech.com/suzhou-vod-dianshiju/大陆剧/147pc/国产剧/20200302/如懿传/如懿传02/index.m3u8
        //http://v1.zxjftech.com/suzhou-vod-dianshiju/大陆剧/147pc/国产剧/20200302/如懿传/如懿传67(1)/index.m3u8
        //http://v1.zxjftech.com/suzhou-vod-dianshiju/大陆剧/147pc/国产剧/20200308/春风十里不如你/春风十里不如你 01_超清/index.m3u8 这种播不了
        //http://v1.zxjftech.com/suzhou-vod-dianshiju/大陆剧/147pc/国产剧/20200308/春风十里不如你/春风十里不如你 02_超清 [DIVX 高质量和大小]/index.m3u8  这种播不了
        // http://v1.zxjftech.com/suzhou-vod-dianshiju/日剧/20200314/轮到你了/轮到你了01/1000kb/hls/index.m3u8
        //http://v1.zxjftech.com/suzhou-vod-dianshiju/美剧/20200325/仁医[第一季18]/良医.The.Good.Doctor.S01E01.中英字幕.WEBrip.720P-人人影视.V2/1000kb/hls/index.m3u8

        $rulesArray = [];
        $vodNames[] = $vodName;
        $vodNames[] = $vodName2;
        if (strpos($m3u8, '仁医[第一季18]') > 0) {
            $rulesArray[] = '仁医[第一季18]/良医.The.Good.Doctor.S01E{nextNum}.中英字幕.WEBrip.720P-人人影视.V2/1000kb/hls/index.m3u8';
            $rulesArray[] = '仁医[第一季18]/良医.The.Good.Doctor.S01E{nextNum}.中英字幕.WEBrip.720P-人人影视/1000kb/hls/index.m3u8';

            $rulesArray[] = '仁医[第一季18]/良医.The.Good.Doctor.S01E{nextNum}.中英字幕.HDTVrip.720P-人人影视.V2/1000kb/hls/index.m3u8';
            $rulesArray[] = '仁医[第一季18]/良医.The.Good.Doctor.S01E{nextNum}.中英字幕.HDTVrip.720P-人人影视/1000kb/hls/index.m3u8';
        } else {
            $rulesArray[] = '{vodName}/{vodName}{nextNum}/{hls}index.m3u8';
            $rulesArray[] = '{vodName}/{vodName}{nextNum}/1000kb/{hls}index.m3u8';
            $rulesArray[] = '{vodName}/{vodName}{nextNum}/2000kb/{hls}index.m3u8';
            $rulesArray[] = '{vodName}/{vodName}{nextNum}(1)/{hls}index.m3u8';
            $rulesArray[] = '{vodName}/{vodName} {nextNum}_超清/{hls}index.m3u8';
            $rulesArray[] = '{vodName}/{vodName} {nextNum}_超清 [DIVX 高质量和大小]/{hls}index.m3u8';
        }
        $hlsReplace = '';
        if (strpos($m3u8, '/hls/') > 0) {
            $hlsReplace = 'hls/';
        }

        $rulesArray = str_replace(
            [' ', '{nextNum}', '{hls}'],
            ['%20', $nextNum, $hlsReplace],
            $rulesArray
        );
        $vodNames = array_unique($vodNames);
        $newVodNames = $vodNames;
        foreach ($vodNames as $vodNameItem) {
            $newVodNames[] = preg_replace('/\d+/', '', $vodName);
        }
        $returnArray = [];


        foreach ($newVodNames as $vodName) {
            $newRulesArray = str_replace('{vodName}', $vodName, $rulesArray);

            $returnArray = array_merge($returnArray, $newRulesArray);
        }
        selfDump($returnArray);
        return $returnArray;
    }

    public function createDongmanPatch($vodNames, $currentNum, $nextNum, $m3u8, $needDealArray)
    {
        $rulesArray = [];
        $rulesArray[] = '/{vodName}/{urlVodName2} {nextNum}/1000kb/{hls}index.m3u8';
        $rulesArray[] = '/{vodName}/{urlVodName2}{nextNum}/1000kb/{hls}index.m3u8';
        $rulesArray[] = '/{vodName}/{vodName}{nextNum}/1000kb/{hls}index.m3u8';

        $rulesArray[] = '/{vodName}/{urlVodName2} {nextNum}/2000kb/{hls}index.m3u8';
        $rulesArray[] = '/{vodName}/{urlVodName2}{nextNum}/2000kb/{hls}index.m3u8';
        $rulesArray[] = '/{vodName}/{vodName}{nextNum}/2000kb/{hls}index.m3u8';
        $hlsReplace = '';
        if (strpos($m3u8, '/hls/') > 0) {
            $hlsReplace = 'hls/';
        }
        selfDump($needDealArray);
        if ($needDealArray[4] == 'index.m3u8' || $needDealArray[4] == 'hls') {
            selfDump(3434343);
            if (strpos($needDealArray[2], '%20') > 0 && ($needDealArray[3] == '1000kb' || $needDealArray[3] == '2000kb')) {
                selfDump("3432423423---=-=-=-=-p-=");
                $urlVodName2 = explode('%20', $needDealArray[2]);
                $urlVodName2 = $urlVodName2[0];
                $vodNames[] = $urlVodName2;
            } else {
                $urlVodName2 = str_replace($currentNum, '', $needDealArray[2]);
            }

        }
        selfDump('urlllflsdjfljasdlfjlsdjlfk' . $urlVodName2);
        $rulesArray = str_replace(['{hls}', '{nextNum}', ' '], [$hlsReplace, $nextNum, '%20'], $rulesArray);
        if (isset($urlVodName2)) {
            $rulesArray = str_replace('{urlVodName2}', $urlVodName2, $rulesArray);
        }

        $vodNames = $this->dealVOdNameQuarter($vodNames);
        $newVodNames = array_filter($vodNames);
        $newVodNames = array_unique($newVodNames);
        $returnArray = [];
        selfDump($vodNames);
        foreach ($newVodNames as $vodName) {
            $returnArray = array_merge($returnArray, str_replace(['{vodName}', '{urlVodName2}'], [$vodName, $vodName], $rulesArray));
        }
        selfDump($returnArray);

        return $returnArray;
    }

    public function createMeijuPatch($vodNames, $currentNum, $nextNum, $m3u8, $needDealArray)
    {
        $rulesArray = [];
        $intNextNum = intval($nextNum);
        $rulesArray[] = '/{vodName}/{urlVodName2}{nextNum}/{hls}index.m3u8';
        $rulesArray[] = '/{vodName}/{vodName}{nextNum}/{hls}index.m3u8';
        $rulesArray[] = '/{vodName}/{urlVodName2}{nextNum}-{intNextNum}/{hls}index.m3u8';
        $rulesArray[] = '/{vodName}/{urlVodName2}{nextNum}-1/{hls}index.m3u8';

        $rulesArray[] = '/{vodName}/{urlVodName2}{nextNum}.End/{hls}index.m3u8';
        $rulesArray[] = '/{vodName}/{vodName}{nextNum}.End/{hls}index.m3u8';

        $rulesArray[] = '/{vodName}/{urlVodName2}{nextNum}.End-1/{hls}index.m3u8';
        $rulesArray[] = '/{vodName}/{vodName}{nextNum}.End-1/{hls}index.m3u8';

        $hlsReplace = '';
        if (strpos($m3u8, '/hls/') > 0) {
            $hlsReplace = 'hls/';
        }
        selfDump($needDealArray);
        if ($needDealArray[3] == 'index.m3u8' || $needDealArray[3] == 'hls') {
            selfDump(3434343);

            $urlVodName2Array = explode($currentNum, $needDealArray[2]);
            array_pop($urlVodName2Array);
            $urlVodName2 = join($currentNum, $urlVodName2Array);
        }
        selfDump('urlllflsdjfljasdlfjlsdjlfk' . $urlVodName2);
        $rulesArray = str_replace(
            ['{hls}', '{nextNum}', ' ', '{intNextNum}'],
            [$hlsReplace, $nextNum, '%20', $intNextNum]
            , $rulesArray);
        if (isset($urlVodName2)) {
            $rulesArray = str_replace('{urlVodName2}', $urlVodName2, $rulesArray);
        }

        $vodNames = $this->dealVOdNameQuarter($vodNames);
        $newVodNames = array_filter($vodNames);
        $newVodNames = array_unique($newVodNames);
        $returnArray = [];
        selfDump($vodNames);
        foreach ($newVodNames as $vodName) {
            $returnArray = array_merge($returnArray, str_replace(['{vodName}', '{urlVodName2}'], [$vodName, $vodName], $rulesArray));
        }
        selfDump($returnArray);

        return $returnArray;
    }

    public function createPath($matchName, $vodNames, $currentNum, $nextNum, $m3u8Url, $needDealArray, $isZonyie)
    {
        $data = $this->createKuku2Path($matchName, $vodNames, $currentNum, $nextNum, $m3u8Url, $needDealArray, $isZonyie);

        return $data;
    }

    public function dealVOdNameQuarter($vodNames)
    {
        $vodNames = array_filter($vodNames);
        $quarterArray = [
            '第一季' => '第1季',
            '第二季' => '第2季',
            '第三季' => '第3季',
            '第四季' => '第4季',
            '第五季' => '第5季',
            '第六季' => '第6季',
            '第七季' => '第7季',
            '第八季' => '第8季',
            '第九季' => '第9季',
            '第十季' => '第10季',
            '第十一季' => '第11季',
            '第十二季' => '第12季',
            '第十三季' => '第13季',
            '第十四季' => '第14季',
            '第十五季' => '第15季',
            '第十六季' => '第16季',
            '第十七季' => '第17季',
            '第十八季' => '第18季',
            '第十九季' => '第19季',
            '第二十季' => '第20季',
        ];

        $newVodNames = str_replace(array_keys($quarterArray), array_values($quarterArray), $vodNames);
        $newVodNames = array_merge($vodNames, $newVodNames);
        $vodNames = str_replace(array_values($quarterArray), array_keys($quarterArray), $vodNames);
        $newVodNames = array_merge($newVodNames, $vodNames);
        $newVodNames2 = str_replace(array_keys($quarterArray), [], $vodNames);
        $newVodNames = array_merge($newVodNames, $newVodNames2);

        $newVodNames2 = str_replace(array_values($quarterArray), [], $vodNames);
        $newVodNames = array_merge($newVodNames, $newVodNames2);

        return array_unique($newVodNames);
    }

    /**
     *  根据1个url生成后续的url
     * @param $m3u8Url string 上一级的url
     * @param $date string '年月日'时间
     * @param $nextNum string 下一集集数
     * @return array
     */
    public function createNextM3u8Urls($m3u8Url, $vodName, $vodName2, $date, $currentNum, $nextNum = '02', $isZonyi)
    {
//        $m3u8Url ='http://v1.zxjftech.com/gz-newgengxin-up2/mov2/zhao/20201125/黑水仙第一季01[1000kb]/index.m3u8' ;
//        $vodName = '黑水仙第一季';
//        $date = date('Ymd');
        $m3u8Urls = [];
        $dateArray = [];
        $nextNum = sprintf('%02d', $nextNum);
        $currentNum = sprintf('%02d', $currentNum);
        $urlArray = parse_url($m3u8Url);
        $domain = $urlArray['scheme'] . "://" . $urlArray['host'];
        $firstDirs = [
            'mov2zhao' => '/gz-newgengxin-up2/mov2/zhao/',//http://v1.zxjftech.com/gz-newgengxin-up2/mov2/zhao/20201125/黑水仙第一季01/index.7中m3u8
            'up2movzhao' => '/gz-newgengxin-up2/mov/zhao/',//http://v1.zxjftech.com/gz-newgengxin-up/mov/zhao/20200726/凡人修仙传02/index.m3u8
            'upmovzhao' => '/gz-newgengxin-up/mov/zhao/',//http://v1.zxjftech.com/gz-newgengxin-up/mov/zhao/20200726/凡人修仙传02/index.m3u8
            'kuku2' => '/gz-newgengxin-up2/kuku2/',
            //http://v1.zxjftech.com/gz-newgengxin-up2/kuku2/20201119/1000/狼殿下/狼殿下cc06集/index.m3u8

            'kuku' => '/gz-newgengxin-up/kuku/',
            //http://v1.zxjftech.com/gz-newgengxin-up/kuku/20200717/1000/地球百子第七季/地球百子第七季05/index.m3u8
            //http://v1.zxjftech.com/gz-newgengxin-up/kuku/20200627/地球百子第七季/06/2000kb/hls/index.m3u8
            //http://v1.zxjftech.com/gz-newgengxin-up/kuku/20200502/上载新生第一季cc01集/index.m3u8

            'movzy' => '/gz-newgengxin-up/mov/zy/',//http://v1.zxjftech.com/gz-newgengxin-up/mov/zy/20200814/地球百子第七季11/index.m3u8
            'upmov2zy' => '/gz-newgengxin-up/mov2/zy/',//http://v1.zxjftech.com/gz-newgengxin-up/mov2/zy/20201208/五哈和他们的朋友.20201113/hls/index.m3u8

        ];
        $vodNames = [];
        $vodNames[] = $vodName;
        $vodNames[] = $vodName2;
        $urlRules = [];
        $match = '';
        selfDump($m3u8Url);

        if (strpos($m3u8Url, '/gz-newgengxin-up/20200325/meiju1/') > 0) {
            $m3u8UrlArray = explode('/gz-newgengxin-up/20200325/meiju1/', $m3u8Url);
//            selfDump($m3u8UrlArray);exit;
            $needDealArray = explode('/', $m3u8UrlArray[1]);
            selfDump($needDealArray);
            $vodNames[] = $needDealArray[1];
            $urlRules = $this->createMeijuPatch($vodNames, $currentNum, $nextNum, $m3u8Url, $needDealArray);
            selfDump($urlRules);
            foreach ($urlRules as $urlRule) {
                $m3u8Urls[] = $m3u8UrlArray[0] . '/gz-newgengxin-up/20200325/meiju1' . $urlRule;
            }

//            selfDump($m3u8Urls);exit;

            return array_unique($m3u8Urls);
        } else if (strpos($m3u8Url, '/suzhou-newgengxin-up/美剧重压/') > 0) {
//            http://v1.zxjftech.com/suzhou-vod-dianying/动漫top/进击的巨人第1季/进击的巨人 01/1000kb/hls/index.m3u8
            $m3u8UrlArray = explode('/suzhou-newgengxin-up/美剧重压/', $m3u8Url);
//            selfDump($m3u8UrlArray);
            $needDealArray = explode('/', $m3u8UrlArray[1]);
            selfDump($needDealArray);
            $vodNames[] = $needDealArray[1];
            $urlRules = $this->createMeijuPatch($vodNames, $currentNum, $nextNum, $m3u8Url, $needDealArray);
            foreach ($urlRules as $urlRule) {
                $m3u8Urls[] = $m3u8UrlArray[0] . '/suzhou-newgengxin-up/美剧重压/' . $needDealArray[0] . $urlRule;
            }

            selfDump($m3u8Urls);

            return array_unique($m3u8Urls);
        } elseif (strpos($m3u8Url, '/suzhou-vod-dianying/') > 0) {
//            http://v1.zxjftech.com/suzhou-vod-dianying/动漫top/进击的巨人第1季/进击的巨人 01/1000kb/hls/index.m3u8
            $m3u8UrlArray = explode('/suzhou-vod-dianying/', $m3u8Url);
//            selfDump($m3u8UrlArray);
            $needDealArray = explode('/', $m3u8UrlArray[1]);
            selfDump($needDealArray);
            $vodNames[] = $needDealArray[1];
            $urlRules = $this->createDongmanPatch($vodNames, $currentNum, $nextNum, $m3u8Url, $needDealArray);
            foreach ($urlRules as $urlRule) {
                $m3u8Urls[] = $m3u8UrlArray[0] . '/suzhou-vod-dianying/' . $needDealArray[0] . $urlRule;
            }
        } elseif (strpos($m3u8Url, '/suzhou-vod-dianshiju/') > 0) {
            selfDump('电视剧 ===');
            $m3u8UrlArray = explode('/suzhou-vod-dianshiju/', $m3u8Url);
//            selfDump($m3u8UrlArray);
            $needDealArray = explode('/', $m3u8UrlArray[1]);
            $currentDate = $needDealArray[3];
            if ($needDealArray[4] == '1000kb') {
                $currentDate = $needDealArray[1];
            }
            if (empty($vodName)) {
                $vodName = $needDealArray[4];
            }
            if (empty($vodName)) {
                return false;
            }
            $m3u8Urls = [];

            $dateArray = $this->createDateArray($currentDate);

            $urlRules = $this->create147PcPath($vodName, $vodName2, $nextNum, $m3u8Url);
//            selfDump('urlRules', $urlRules);

            foreach ($dateArray as $date2) {
                foreach ($urlRules as $urlRule) {
                    $m3u8Urls[] = $m3u8UrlArray[0] . '/suzhou-vod-dianshiju/' . $needDealArray[0] . '/' . $needDealArray[1] . '/' . $needDealArray[2] . '/' . $date2 . '/' . $urlRule;
                    $m3u8Urls[] = $m3u8UrlArray[0] . '/suzhou-vod-dianshiju/' . $needDealArray[0] . '/' . $date2 . '/' . $urlRule;
                    $m3u8Urls[] = $m3u8UrlArray[0] . '/suzhou-vod-dianshiju/' . $date2 . '/' . $urlRule;
                }
            }
        } else {
            foreach ($firstDirs as $machName => $machValue) {
                if (strpos($m3u8Url, $machValue) > 0) {
                    $m3u8UrlArray = explode($machValue, $m3u8Url);
                    $needDealArray = explode('/', $m3u8UrlArray[1]);
                    $currentDate = $needDealArray[0];

                    $urlRuleArray = $this->createPath($machName, $vodNames, $currentNum, $nextNum, $m3u8Url, $needDealArray, $isZonyi);
                    selfDump($urlRuleArray, '==-=-=-=-=-=-');
                    $urlRules = array_merge($urlRules, $urlRuleArray);
                }
            }

            selfDump($urlRules);
            if (empty($urlRules)) {
                return false;
            }
            $dateArray = $this->createDateArray($currentDate);
//        加入今天、昨天、明天
            $newDateArray = [
                date('Ymd', strtotime('yesterday')),
                date('Ymd', strtotime('today')),
            ];
            $dateArray = array_merge($dateArray, $newDateArray);
            $dateArray = array_unique($dateArray);

            //创建kuku2路径
            foreach ($dateArray as $date2) {
                foreach ($urlRules as $urlRule) {
                    foreach ($firstDirs as $dir) {
                        $m3u8Urls[] = $domain . $dir . $date2 . $urlRule;
                    }
                }
            }
        }
//        selfDump($m3u8Urls);exit;
        if (empty($m3u8Urls)) {
            return false;
        }
        return array_unique($m3u8Urls);
    }

    public function dealTiantangByVodUpdate()
    {
        exit;
        $timeStart = microtime(true);
        selfDump('从vod表获取更新数据，执行定时更新检测开始');
        $vodIds = db('vod')->where(['type_id|type_id_1' => ['in', [2, 3, 4]]])->whereTime('vod_time', '>=', time() - 7200)->order('vod_id', 'asc')->column('vod_id');
//        $vodIds = [85255, 84371, 85647, 96057, 85338, 95971, 83550, 96049, 95965];
//        $vodIds=[96057];
        foreach ($vodIds as $vodId) {
            $this->dealOneVodTiantang($vodId);
        }
        selfDump('从vod表获取更新数据，执行定时更新检测结束');
        $timeEnd = microtime(true);
        selfDump('共计耗时' . ($timeEnd - $timeStart) . '秒');
    }

    public function dealTiantangByOtherSetting()
    {
        sendDingding('天堂抓取任务开始执行', '天堂影视通知');
        $timeStart = microtime(true);
        selfDump('从other_setting表获取数据，执行定时更新检测开始');
        $vodIds = db('m3u8_other_setting')->where('is_lianzai', 1)->order('id', 'desc')->column('vod_id', 'id');
        foreach ($vodIds as $vodId) {
            $this->fixedkongurl($vodId);
            $this->dealOneVodTiantang($vodId);
        }
        selfDump('从other_setting表获取数据，执行定时更新检测结束');
        $timeEnd = microtime(true);
        $times = ($timeEnd - $timeStart);
        selfDump('共计耗时' . $times . '秒');
        sendDingding('天堂抓取任务开始结束' . '共计耗时' . $times . '秒', '天堂影视通知');
    }

    public function dealOneVodTiantang($vodId)
    {
        //查询是否要连载
        $otherSetting = db('m3u8_other_setting')->where('from', 'tiantang')->where('vod_id', $vodId)->find();
        if (empty($otherSetting)) {
            selfDump('未找到天堂设置，跳过 vodId=' . $vodId);
            return false;
        }
        if ($otherSetting['is_lianzai'] < 1) {
            selfDump('天堂设置为不连载，跳过 vodId=' . $vodId);
            return false;
        }
        selfDump('当前要出里的vodId=' . $vodId . "  名称" . $otherSetting['vod_name']);
        //得到电视剧剧集信息
        $vodInfo = db('vod')->where('vod_id', $vodId)->field('vod_id,vod_name,type_id_1,vod_play_url,vod_pic_thumb,vod_pic_slide')->find();
        if (empty($vodInfo)) {
            selfDump('未能查询到播放列表，跳过 vodId=' . $vodId);
            return false;
        }

        //处理图片
        if (empty($vodInfo['vod_pic_slide']) || empty($vodInfo['vod_pic_thumb'])) {
            $this->dealVodImage($vodId, $otherSetting['tiantang_id']);
        }

        $playGroups = [];
        $playUrls = explode('$$$', $vodInfo['vod_play_url']);
        foreach ($playUrls as $key => $jujiurl) {
            //解析url
            $jujis = explode('#', $jujiurl);
            $jujis = array_filter($jujis);
            $playList = [];
            foreach ($jujis as $juji) {
                list($name, $url) = explode('$', $juji);
                $playList[] = [
                    'name' => $name,
                    'url' => $url,
                ];
            }
            $playGroups[] = $playList;
        }
        //多余一个播放列表时按照集数从多到少进行排序
        if (count($playGroups) > 1) {
            usort($playGroups, function ($a, $b) {
                if (count($a) < count($b)) {
                    return 1;
                } elseif (count($a) < count($b)) {
                    return -1;
                } else {
                    return 0;
                }
            });
        }
        //排序得到集数最多的
        $playList = $playGroups[0];
        selfDump($playList);
        $jujiCount = count($playList); //平台剧集的数量
        $tiantangList = db('m3u8_cache_other')->where('from', 'tiantang')->where('vod_id', $vodId)->order('sort', 'asc')->column('sort,key,value,id,is_need_deal', 'sort');
        selfDump('天堂list', $tiantangList);
        $tiantangListCount = db('m3u8_cache_other')->where('from', 'tiantang')->where('vod_id', $vodId)->order('sort', 'asc')->where('is_need_deal', 0)->count() ?: 0;; //已处理的天堂app的剧集数量
        //得到天堂的集数数据
        $TiantangAppDetailUrl = "https://ss.lcjlnjyy.com:11011/system/api/video/detail/source/" . $otherSetting['tiantang_id'] . "?channel=233&idfa=00000000-0000-0000-0000-000000000000&ifrom=ios&packageName=com.tian.dian.ying.tian.product&version=1.0.0";

        $jsonString = curl_get_file_contents($TiantangAppDetailUrl);
        $jsonArray = json_decode($jsonString, true);
        $tiantangAppCount = count($jsonArray['data']); //天堂app的剧集数量

        selfDump('天堂app数量' . $tiantangAppCount);
        selfDump('已处理天堂的剧集数量' . $tiantangListCount);
        selfDump('平台的剧集数量' . $jujiCount);
        selfDump('比较结果' . (($tiantangListCount < $tiantangAppCount) ? 1 : 0));
        //得到 天堂app数量 大于等于 平台数量 ，已处理的天堂的剧集数量小于天堂app数量则 进行处理
        if ($tiantangListCount < $tiantangAppCount) {
            foreach ($jsonArray['data'] as $key => $jsonItem) {
                selfDump($jsonItem['name'] . ' 正在处理 ' . $key);
                if (isset($tiantangList[$key]) && $tiantangList[$key]['is_need_deal'] == 0) {
                    if ($tiantangList[$key]['key'] != $jsonItem['name']) {
                        db('m3u8_cache_other')->where('id', $tiantangList[$key]['id'])->update(['key' => $jsonItem['name']]);
                    }
                    continue;
                } else {
                    if (!isset($tiantangList[$key - 1])) {
                        selfDump('当前电影' . $vodId . ' [' . $otherSetting['vod_name'] . '] 当前集数' . ($key + 1) . ' ' . $jsonItem['name'] . '在获取天堂上一条数据时出错');
                        sendDingding('当前电影' . $vodId . ' [' . $otherSetting['vod_name'] . '] 当前集数' . ($key + 1) . ' ' . $jsonItem['name'] . ' 在获取天堂上一条数据时出错 ,跳过', '天堂影视通知');
                        break;
                    }

                    $preMeu8Url = $tiantangList[$key - 1]['value'];
                    if (empty($preMeu8Url)) {
                        selfDump('当前电影' . $vodId . ' [' . $otherSetting['vod_name'] . '] 当前集数' . ($key + 1) . ' ' . $jsonItem['name'] . ' 上一集数据为空');
                        if (isset($tiantangList[$key])) {
                            sendDingding('当前电影' . $vodId . ' [' . $otherSetting['vod_name'] . '] 当前集数' . ($key + 1) . ' ' . $jsonItem['name'] . ' 上一集数据为空 ,跳过', '天堂影视通知');
                        }
                        break;
                    }
                    $nextM3u8Urls = $this->createNextM3u8Urls($preMeu8Url, $otherSetting['vod_name'], $otherSetting['tiantang_name'], date('Y-m-d'), $key, $key + 1, in_array($vodInfo['type_id_1'], [3]));
                    selfDump('========', $nextM3u8Urls);

                    if ($nextM3u8Urls === false) {
                        $insertArray = [
                            'vod_id' => $vodId,
                            'vod_name' => $otherSetting['vod_name'],
                            'sort' => $key,
                            'key' => $jsonItem['name'],
                            'value' => '',
                            'is_need_deal' => 1,
                            'time_added' => time(),
                            'from' => 'tiantang'
                        ];
                        try {
                            $errorId = db('m3u8_cache_other')->insertGetId($insertArray);
                            selfDump('当前电影' . $vodId . ' [' . $otherSetting['vod_name'] . '] 当前集数' . $key . ' ' . $jsonItem['name'] . '在处理url规则时出错');
                            sendDingding('当前电影' . $vodId . ' [' . $otherSetting['vod_name'] . '] 当前集数' . $key . ' ' . $jsonItem['name'] . ' 在处理url规则时出错 ,跳过' . "\r\n" . 'http://n1.saok.xyz/app.php/tiankong/listinfo/id/' . $errorId . '.html', '天堂影视通知', ['17705336300','18560252112']);
                        } catch (\Exception $e) {

                        }
                        break;
                    }
//                    selfDump('nextM3u8urls', $nextM3u8Urls);
                    $is_need_deal = 1;
                    foreach ($nextM3u8Urls as $numsber => $nextM3u8Url) {
                        selfDump($numsber, $nextM3u8Url);
                        $return = curl_get_file_contents($nextM3u8Url);
                        selfDump($return);
                        if ($return !== false && (strpos($return, 'm3u8') > 0 || strpos($return, 'EXTM3U') > 0)) {
                            $is_need_deal = 0;
                            selfDump("99989ewr89r89e8r98r9e89er");
                            break;
                        }
                    }
                    selfDump('当前获取到的url' . ($nextM3u8Url ?: ' --- null '));
                    if ($is_need_deal == 1 && !isset($tiantangList[$key])) {
                        $insertArray = [
                            'vod_id' => $vodId,
                            'vod_name' => $otherSetting['vod_name'],
                            'sort' => $key,
                            'key' => $jsonItem['name'],
                            'value' => '',
                            'is_need_deal' => $is_need_deal,
                            'time_added' => time(),
                            'from' => 'tiantang'
                        ];
                        $errorId = db('m3u8_cache_other')->insertGetId($insertArray);
                        selfDump('未能处理 天堂影视 ' . $vodId . ' ' . $otherSetting['vod_name'] . ' ' . ($key + 1) . ' ' . $jsonItem['name'] . ' 的数据');
                        if ($jujiCount - 1 > $key) {
                            $otherMsg = ' 需要人工介入  后面还有' . ($jujiCount - $key - 1) . '集未处理';
                            $atMobile = [];
                        } else {
                            $otherMsg = ' 需要人工介入 这是目前更新的最后一集 ';
                            $atMobile = ['17705336300','18560252112'];
                        }
                        sendDingding('未能处理 天堂影视 ' . $vodId . ' ' . $otherSetting['vod_name'] . ' ' . ($key + 1) . ' ' . $jsonItem['name'] . ' 的数据 ' . $otherMsg . ' 总集数是' . $jujiCount . ',跳过  ' . "\r\n" . 'http://n1.saok.xyz/app.php/tiankong/listinfo/id/' . $errorId . '.html', '天堂影视通知', $atMobile);
                        break;
                    } else {
                        $insertArray = [
                            'vod_id' => $vodId,
                            'vod_name' => $otherSetting['vod_name'],
                            'sort' => $key,
                            'key' => $jsonItem['name'],
                            'value' => $nextM3u8Url,
                            'is_need_deal' => $is_need_deal,
                            'time_added' => time(),
                            'from' => 'tiantang'
                        ];
                    }
                    if (isset($tiantangList[$key])) {
                        selfDump('存在¥key', $is_need_deal);
                        if ($is_need_deal == 0) {
                            db('m3u8_cache_other')->where('id', $tiantangList[$key]['id'])->update($insertArray);
                            $tiantangList[$key] = array_merge($tiantangList[$key], $insertArray);
                            selfDump('当前电影' . $vodId . ' [' . $otherSetting['vod_name'] . '] 当前集数' . $key . ' ' . $jsonItem['name'] . ' 更新成功');
                        } else {
                            selfDump('当前电影' . $vodId . ' [' . $otherSetting['vod_name'] . '] 当前集数' . $key . ' ' . $jsonItem['name'] . ' 更新数据失败 ,跳过');
                            sendDingding('当前电影' . $vodId . ' [' . $otherSetting['vod_name'] . '] 当前集数' . $key . ' ' . $jsonItem['name'] . ' 更新数据失败 ,跳过' . "\r\n" . 'http://n1.saok.xyz/app.php/tiankong/listinfo/id/' . $tiantangList[$key]['id'] . '.html', '天堂影视通知', ['17705336300','18560252112']);

                        }
                    } else {
                        selfDump($insertArray);
                        $id = db('m3u8_cache_other')->insertGetId($insertArray);
                        $insertArray['id'] = $id;
                        $tiantangList[$key] = $insertArray;
                        selfDump('当前电影' . $vodId . ' [' . $otherSetting['vod_name'] . '] 当前集数' . ($key + 1) . ' ' . $jsonItem['name'] . ' 插入成功');
                    }


                }

            }
        } else {
            selfDump('电影' . $vodInfo['vod_name'] . '[' . $vodInfo['vod_id'] . '] 【天堂 ' . $otherSetting['tiantang_id'] . '  ' . $otherSetting['tiantang_name'] . '】没有更多剧集 跳过');
        }


    }


    public function search($keyword, $page = 1)
    {
        $url="https://ss.lcjlnjyy.com:11011/system/api/video/search?channel=meijupingios&idfa=00000000-0000-0000-0000-000000000000&ifrom=ios&keyword=".urlencode($keyword)."&packageName=com.meijuniao.app&pageNum=".$page."&pageSize=10&version=2.2.3";

        $data = curl_get_file_contents($url, [
            'appId' => '200',
            'appStoreid' => '100000',
            'Cookie' => '',
            'charset' => 'utf-8'
        ],'User-Agent: mei ju tian tang/2.2.3 (iPhone; iOS 14.3; Scale/2.00)');
        if (IS_CLI) {
            selfDump($data);
        } else {
            return $data;
        }

    }

    public function matchVod(){
        $vodId = input('id',0,'intval');
        if($vodId<1) {
            $this->error('数据有误！');
        }
            $iscunzai = db('m3u8_other_setting')->where(["vod_id" => $vodId])->find();
            if($iscunzai){
                $msg = "数据已处理";
            }else{
                $vodInfo = db('vod')->where('vod_id',$vodId)->field("vod_id,vod_name,vod_actor,vod_director")->find();
                if(empty($vodInfo)){
                    $this->error('获取影片信息失败！');
                }

                $searchData = $this->search($vodInfo['vod_name']);
                if($searchData===false){
                    $this->error('搜索关键词失败');
                }

                $searchData = json_decode($searchData,true);
                if(empty($searchData['data']) || !is_array($searchData['data'])){
                    $this->error('查询数据为空');
                }
                $matchData =[];
                foreach ($searchData['data'] as $item){
                    if($item['name']==$vodInfo['vod_name']){
                        $matchData[]=$item;
                    }elseif($item['director']==$vodInfo['vod_director']){
                        $matchData[]=$item;
                    }
                }
                if(empty($matchData)){
                    $this->error('没有匹配到数据');
                }
                $msg ="";
                foreach ($matchData as $match){
                    if(!empty($msg)){
                        $msg.="<br/>";
                    }
                    $msg.="<a href='/app.php/tiankong/info?tiantang_id=".$match['id']."&vod_id=".$vodInfo['vod_id']."&tiantang_name=".$match['name']."&vod_name=".$vodInfo['vod_name']."'>".$match['name']."[id:".$match['id']."-".$match['director']."]</a>";
                }

            }
            $this->success($msg);

    }

    public function dealVodImage($vodId, $tiantangId)
    {
        $tiantangDetailUrl = "https://ss.lcjlnjyy.com:11011/system/api/video/detail/" . $tiantangId . "?categoryId=0&channel=233&idfa=00000000-0000-0000-0000-000000000000&ifrom=ios&packageName=com.tian.dian.ying.tian.product&userId=0&version=1.0.0";
        $content = curl_get_file_contents($tiantangDetailUrl, [
            'appId' => '233',
            'appStoreid' => '1511099129',
            'Cookie' => '',
            'charset' => 'utf-8'
        ]);

        $jsonContent = json_decode($content, true);
        if (json_last_error() === JSON_ERROR_NONE) {
            selfDump('获取到天堂详情信息，等待处理');
            if (isset($jsonContent['data']) && isset($jsonContent['data']['hLogo']) && isset($jsonContent['data']['sLogo'])) {
                selfDump('更新图片');
                db('vod')->where('vod_id', $vodId)->update([
                    "vod_pic_thumb" => $jsonContent['data']['sLogo'],
                    "vod_pic_slide" => $jsonContent['data']['hLogo']
                ]);
            } else {
                selfDump('没能获取到图片', $jsonContent);
            }
        } else {
            selfDump('更新图片失败');
        }
    }


    public function dealVodImgFromTiantang()
    {
        $list = db('m3u8_other_setting')->where('from', 'tiantang')->order('id', 'asc')->select();
        foreach ($list as $tiantangItem) {
            $vodInfo = db('vod')->where('vod_id', $tiantangItem['vod_id'])->field('vod_pic_thumb,vod_pic_slide')->find();

            if (empty($vodInfo['vod_pic_thumb']) || empty($vodInfo['vod_pic_slide'])) {
                $this->dealVodImage($tiantangItem['vod_id'], $tiantangItem['tiantang_id']);
            } else {
                selfDump('不需要更新表中的图片');
            }

        }
    }

    public function fixedkongurl($vodId)
    {

        // $vodId = '81638';

        //查询是否要连载
        $otherSetting = db('m3u8_other_setting')->where('from', 'tiantang')->where('vod_id', $vodId)->find();


        if (empty($otherSetting)) {
            selfDump('未找到天堂设置，跳过 vodId=' . $vodId);
            return false;
        }
        //处理图片
        if (empty($vodInfo['vod_pic_slide']) || empty($vodInfo['vod_pic_thumb'])) {
            $this->dealVodImage($vodId, $otherSetting['tiantang_id']);
        }
        if ($otherSetting['is_lianzai'] < 1) {
            selfDump('天堂设置为不连载，跳过 vodId=' . $vodId);
            return false;
        }
        selfDump('当前要出里的vodId=' . $vodId . "  名称" . $otherSetting['vod_name']);
        //得到电视剧剧集信息
        $vodInfo = db('vod')->where('vod_id', $vodId)->field('vod_id,vod_name,type_id_1,vod_play_url,vod_pic_thumb,vod_pic_slide')->find();
        if (empty($vodInfo)) {
            selfDump('未能查询到播放列表，跳过 vodId=' . $vodId);
            return false;
        }



        $playGroups = [];
        $playUrls = explode('$$$', $vodInfo['vod_play_url']);
        foreach ($playUrls as $key => $jujiurl) {
            //解析url
            $jujis = explode('#', $jujiurl);
            $jujis = array_filter($jujis);
            $playList = [];
            foreach ($jujis as $juji) {
                list($name, $url) = explode('$', $juji);
                $playList[] = [
                    'name' => $name,
                    'url' => $url,
                ];
            }
            $playGroups[] = $playList;
        }
        //多余一个播放列表时按照集数从多到少进行排序
        if (count($playGroups) > 1) {
            usort($playGroups, function ($a, $b) {
                if (count($a) < count($b)) {
                    return 1;
                } elseif (count($a) < count($b)) {
                    return -1;
                } else {
                    return 0;
                }
            });
        }
        //排序得到集数最多的
        $playList = $playGroups[0];
        selfDump($playList);
        $jujiCount = count($playList); //平台剧集的数量
        $tiantangList = db('m3u8_cache_other')->where('from', 'tiantang')->where('vod_id', $vodId)->order('sort', 'asc')->column('sort,key,value,id,is_need_deal', 'sort');
        selfDump('天堂list', $tiantangList);
        $tiantangListCount = db('m3u8_cache_other')->where('from', 'tiantang')->where('vod_id', $vodId)->order('sort', 'asc')->where('is_need_deal', 0)->count() ?: 0;; //已处理的天堂app的剧集数量
        //得到天堂的集数数据
        $TiantangAppDetailUrl = "https://ss.lcjlnjyy.com:11011/system/api/video/detail/source/" . $otherSetting['tiantang_id'] . "?channel=233&idfa=00000000-0000-0000-0000-000000000000&ifrom=ios&packageName=com.tian.dian.ying.tian.product&version=1.0.0";

        $jsonString = curl_get_file_contents($TiantangAppDetailUrl);
        $jsonArray = json_decode($jsonString, true);
        $tiantangAppCount = count($jsonArray['data']); //天堂app的剧集数量

        selfDump('天堂app数量' . $tiantangAppCount);
        selfDump('已处理天堂的剧集数量' . $tiantangListCount);
        selfDump('平台的剧集数量' . $jujiCount);
        selfDump('比较结果' . (($tiantangListCount < $tiantangAppCount) ? 1 : 0));
        //得到 天堂app数量 大于等于 平台数量 ，已处理的天堂的剧集数量小于天堂app数量则 进行处理
        if ($tiantangListCount < $tiantangAppCount) {


            $i = $tiantangListCount -1;

            for ($i; $i <= $jujiCount; $i++) {


                $ding_map['vod_id'] = $vodId;
                $ding_map['sort'] = $i;
                selfDump($ding_map);

                $list = db('m3u8_cache_other')->where($ding_map)->find();
                selfDump($list);

                if ($list == null) {
                    selfDump('********************************************************');
                    selfDump('list kongle');
                    selfDump('********************************************************');
                    return ;

                }

//                $newurl =$this->fixdate($list[value],1);
//                $newurl1 =$this->fixdate($list[value],-1);

                selfDump($list);
                //获取当前要爬取url的时间距离今天多少天
                $kaishishijian = $this->fixdangqiantime($list['value']);
                selfDump($kaishishijian);


                if ($kaishishijian['shichang'] == null || $kaishishijian['shichang'] > 500) {
                    //链接里面没有时间
                    selfDump($list);
                    selfDump($list['value']);
                    selfDump('2222');
                    $newurl = $this->fixjishu($list['value'], $list['sort'] + 1);
                    selfDump($newurl);
                    selfDump('111111111');

                    $return = curl_get_file_contents($newurl);
                    if ($return !== false && (strpos($return, 'm3u8') > 0 || strpos($return, 'EXTM3U') > 0)) {
                        selfDump($newurl);
                        $date['vod_id'] = $vodId;
                        $date['sort'] = $list['sort'] + 1;
                        $map_date = db('m3u8_cache_other')->where($date)->find();

                        if ($map_date) {
                            $date['value'] = $newurl;
                            $date['is_need_deal'] = 0;
                            selfDump($map_date['id']);
                            $errorId = db('m3u8_cache_other')->where('id', $map_date['id'])->update($date);

                            selfDump('更新成功', $errorId);
                        } else {
                            $date['vod_name'] = $list['vod_name'];
                            $date['from'] = 'tiantang';
                            $date['key'] = $list['sort'] + 2;
                            $date['value'] = $newurl;
                            $date['time_added'] = time();
                            selfDump($date);
                            $errorId = db('m3u8_cache_other')->insertGetId($date);
                            selfDump('插入成功', $errorId);
                        }


                    }


                } else {

                    for ($i_shi = 0; $i_shi < $kaishishijian['shichang']; $i_shi++) {
                        //根据当前url日期链接爬取集数

                        $newurl = $this->fixdate($list[value], $i_shi);

                        $newurl = $this->fixjishu($newurl, $list['sort'] + 1);

                        if ($newurl) {
                            selfDump($newurl);
                            $date['vod_id'] = $vodId;
                            $date['sort'] = $list['sort'] + 1;
                            $map_date = db('m3u8_cache_other')->where($date)->find();

                            if ($map_date) {
                                $date['value'] = $newurl;
                                $date['is_need_deal'] = 0;
                                selfDump($map_date['id']);
                                $errorId = db('m3u8_cache_other')->where('id', $map_date['id'])->update($date);

                                selfDump('更新成功', $errorId);
                            } else {
                                $date['vod_name'] = $list['vod_name'];

                                $date['from'] = 'tiantang';
                                $date['key'] = $list['sort'] + 2;
                                $date['value'] = $newurl;
                                $date['time_added'] = time();
                                //selfDump($date);
                                $errorId = db('m3u8_cache_other')->insertGetId($date);
                                selfDump('插入成功', $errorId);
                            }


                            break;
                        }

                    }
                }

            }


        }

    }


    /*
     * $url是链接
     *
     * 返回url中的时间 ,时长
     */
    public function fixdangqiantime($url)
    {
        $str1 = $url;
        $regex1 = "'[1-9][0-9]{4,14}'";
        preg_match_all($regex1, $str1, $shijian);
        $okd = $shijian[0][0];//获取到时间目录

        $birth = strtotime($okd);
        $second = time() - $birth;
        $ij = ceil($second / (24 * 3600));
//        selfDump($okd);
//        selfDump($ij);
        $data['time'] = $okd;
        $data['shichang'] = $ij;
        if ($data['time'] == null) {
            selfDump($url);
        }
        return $data;
    }

    /*
     * $url是链接
     * $num是1增加一天
     * 返回url
     */
    public function fixdate($url, $num)
    {

        //获取到集数先匹配年月日(正则匹配)
        $str1 = $url;
        $regex1 = "'[1-9][0-9]{4,14}'";
        preg_match_all($regex1, $str1, $shijian);
        $okd = $shijian[0][0];//获取到时间目录
        //增加一天，并不超过当天
        //转换时间戳
        $shijianchuo = strtotime($okd);
//        selfDump ($okd);
//        selfDump ($shijianchuo);

        if ($num > 0) {
            selfDump($num);
            $jiayitian = ($num * 86400) + $shijianchuo;
            $jiayi = date('Ymd', $jiayitian);//时间自增
        } else {
            $jiayi = date('Ymd', $shijianchuo);
        }


        //替换链接URL


        $regex1 = "'[1-9][0-9]{4,14}'";
        $msg = preg_replace($regex1, $jiayi, $str1);
//        selfDump($msg);
        return $msg;

    }

    public function fixdate_jian($url, $num)
    {

        //获取到集数先匹配年月日(正则匹配)
        $str1 = $url;
        $regex1 = "'[1-9][0-9]{4,14}'";
        preg_match_all($regex1, $str1, $shijian);
        $okd = $shijian[0][0];//获取到时间目录
        //增加一天，并不超过当天
        //转换时间戳
        $shijianchuo = strtotime($okd);
//        selfDump ($okd);
//        selfDump ($shijianchuo);

        if ($num > 0) {
            selfDump($num);
            $jiayitian = $shijianchuo - ($num * 86400);
            $jiayi = date('Ymd', $jiayitian);//时间自增
        } else {
            $jiayi = date('Ymd', $shijianchuo);
        }


        //替换链接URL


        $regex1 = "'[1-9][0-9]{4,14}'";
        $msg = preg_replace($regex1, $jiayi, $str1);
//        selfDump($msg);
        return $msg;

    }

    /*
 * $url是链接
 *
 * 返回url增加一集
 */
    public function fixjishu($url, $sort)
    {

//        $url= "http://v1.zxjftech.com/gz-newgengxin-up2/kuku2/20201116/1000/鹿鼎记2020/鹿鼎记2020cc06集/index.m3u8";
//        $url= "http://v1.zxjftech.com/gz-newgengxin-up2/kuku2/20201106/1000/隐秘而伟大/隐秘而伟大cc06集/index.m3u8";
//        $url= "http://v1.zxjftech.com/gz-newgengxin-up2/mov2/zhao/20201126/天空市凶案第一季06[1000kb]/index.m3u8";
//        $url= "http://v1.zxjftech.com/gz-newgengxin-up2/kuku2/20200930/2000/地球百子第1季/地球百子第1季06/index.m3u8";
//        $url= "http://v1.zxjftech.com/gz-newgengxin-up2/mov2/zhao/20201204/空乘危机05[1000kb]/index.m3u8";

        selfDump($url);
        $str1 = $url;
        $regex1 = "'[1-9][0-9]{4,14}'";
        preg_match_all($regex1, $str1, $shijian);
        $okd = $shijian[0][0];//获取到时间目录
        selfDump($okd);
        selfDump($sort);
//        $abc=substr($okd,-2);
//        if($abc==$sort){
//            $url=$this->fixdate($url,1);
//        }
//        selfDump($url);

        if ($sort < 10) {
            $sort = '0' . $sort;
        }
        $caixiang[] = $sort . "/";
        $caixiang[] = $sort . "集";
        $caixiang[] = $sort . "[";
        $caixiang[] = $sort . "%";
        $caixiang[] = $sort . ".";
        $caixiang[] = $sort . "-";
        $caixiang[] = $sort . "]";
        $caixiang[] = $sort . "_";
        $caixiang[] = $sort . "H";

        selfDump($caixiang);
        foreach ($caixiang as $caixianglist) {
            selfDump('ooooooooo');
            selfDump($url);
            selfDump('$caixianglist'.$caixianglist);


            $a = strripos($url, $caixianglist);

            selfDump('$a'.$a);
            if ($a) {


                $num_sort = $sort + 1;

                if ($num_sort < 10) {
                    $list_vodid[key] = '0' . $num_sort;
                } else {
                    $list_vodid[key] = $num_sort;
                }


                if ($sort + 1 > 100) {

                    selfDump('sort'.$sort);
                    $chang = 3;
                } else {
                    $chang = 2;
                    selfDump('sort'.$sort);
                }
                selfDump('chang'.$chang);
                selfDump('$a'.$a);
                $urla = substr_replace($url, $list_vodid[key], $a, $chang);
                selfDump($urla);
                $return = curl_get_file_contents($urla);
                if ($return !== false && (strpos($return, 'm3u8') > 0 || strpos($return, 'EXTM3U') > 0)) {

                    return $urla;
                }

            }

        }





    }




}